unit ReportPrepare;

interface

uses
  Classes, SysUtils, Controls,
  RaApplication, RaBase, RaControlsVCL, DataModule, DB, ADODB;

type
  TfrmReportPrepare = class(TRaFormCompatible)
    UnitsGrid: TRaDBGrid;
    SelectedList: TRaListBox;
    btnAdd: TRaBitButton;
    btnNext: TRaBitButton;
    lblCaption: TRaLabel;
    DataSource: TDataSource;
    btnBack: TRaBitButton;
    RaLabel1: TRaLabel;
    btnSelectAll: TRaBitButton;
    btnRemove: TRaBitButton;
    btnDeleteAll: TRaBitButton;
    Query: TADOQuery;
    Timer: TRaIntervalTimer;
    RaApplicationEvents1: TRaApplicationEvents;
    procedure FormShow(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnBackClick(Sender: TObject);
    procedure btnRemoveClick(Sender: TObject);
    procedure btnSelectAllClick(Sender: TObject);
    procedure btnDeleteAllClick(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure TimerTick(Sender: TObject);
    procedure UnitsGridClick(Sender: TObject);
    procedure RaApplicationEvents1ScreenResize(Sender: TObject);
  private
    Units: TStringList;
    OldWidth, OldHeight: integer;
  public
    procedure AddItemToList(PosNum: integer);
    function GetPosNumFromList(s: string): integer;
  end;

var
  frmReportPrepare: TfrmReportPrepare;

implementation

uses ReportParams, Units;

{$R *.dfm}

procedure TfrmReportPrepare.AddItemToList(PosNum: integer);
var
  i: integer;
begin
  for i := 0 to SelectedList.Items.Count - 1 do
    if GetPosNumFromList(SelectedList.Items.Strings[i]) = PosNum then exit;
  SelectedList.Items.Add(IntToStr(PosNum) + ' - ' + Query.FieldValues['name']);
  Units.Add(IntToStr(PosNum));
end;

procedure TfrmReportPrepare.btnAddClick(Sender: TObject);
begin
  AddItemToList(Query.FieldValues['POS']);
  btnNext.Visible := not (SelectedList.Items.Count = 0);
  SelectedList.ItemIndex := SelectedList.Items.Count - 1;
end;

procedure TfrmReportPrepare.btnDeleteAllClick(Sender: TObject);
begin
  SelectedList.Items.Clear;
  Units.Clear;
  btnNext.Visible := not (SelectedList.Items.Count = 0);
end;

procedure TfrmReportPrepare.btnRemoveClick(Sender: TObject);
var
  ItemIndex: integer;
begin
  ItemIndex := SelectedList.ItemIndex;
  if ItemIndex = -1 then exit;
  SelectedList.Items.Delete(ItemIndex);
  Units.Delete(ItemIndex);
  if not (SelectedList.Items.Count = 0) then
    SelectedList.ItemIndex := SelectedList.Items.Count - 1;
  btnNext.Visible := not (SelectedList.Items.Count = 0);
end;
  
procedure TfrmReportPrepare.FormActivate(Sender: TObject);
begin
  Query.Active := true;
end;

procedure TfrmReportPrepare.FormDeactivate(Sender: TObject);
begin
  Query.Active := false;
end;

procedure TfrmReportPrepare.FormHide(Sender: TObject);
begin
  Units.Free;
end;

procedure TfrmReportPrepare.FormShow(Sender: TObject);
begin
  Query.Active := true;
  (UnitsGrid.Columns.Items[0] as TRaDBGridColumn).Width := Round(Width*0.15);
  (UnitsGrid.Columns.Items[1] as TRaDBGridColumn).Width := Round(Width*0.5);
  (UnitsGrid.Columns.Items[2] as TRaDBGridColumn).Width := Round(Width*0.2);
  (UnitsGrid.Columns.Items[3] as TRaDBGridColumn).Width := Round(Width*0.2);
  btnNext.Visible := false;
  SelectedList.Items.Clear;
  Units := TStringList.Create;
  OldWidth := frmReportPrepare.Width;
  OldHeight := frmReportPrepare.Height;
end;

function TfrmReportPrepare.GetPosNumFromList(s: string): integer;
var
  NumPos: integer;
begin
  NumPos := Pos(' - ', s);
  Result := StrToInt(copy(s, 1, NumPos-1))
end;

procedure TfrmReportPrepare.RaApplicationEvents1ScreenResize(Sender: TObject);
begin
    (UnitsGrid.Columns.Items[0] as TRaDBGridColumn).Width := Round(Application.EventRectangle.Right*0.15);
    (UnitsGrid.Columns.Items[1] as TRaDBGridColumn).Width := Round(UnitsGrid.Width*0.5);
    (UnitsGrid.Columns.Items[2] as TRaDBGridColumn).Width := Round(UnitsGrid.Width*0.2);
    (UnitsGrid.Columns.Items[3] as TRaDBGridColumn).Width := Round(UnitsGrid.Width*0.2);
end;

procedure TfrmReportPrepare.TimerTick(Sender: TObject);
begin
  Timer.Enabled := false;
end;

procedure TfrmReportPrepare.UnitsGridClick(Sender: TObject);
begin
{  if not Timer.Enabled then
    Timer.Enabled := true
  else
  begin
    Timer.Enabled := false;   }
    btnAddClick(nil);
{  end;   }
end;

procedure TfrmReportPrepare.btnBackClick(Sender: TObject);
begin
  close;
end;

procedure TfrmReportPrepare.btnNextClick(Sender: TObject);
begin
  frmReportParams.Units := Units;
  frmReportParams.Show;
end;

procedure TfrmReportPrepare.btnSelectAllClick(Sender: TObject);
begin
  Query.First;
  while not Query.Eof do
  begin
    AddItemToList(Query.FieldValues['POS']);
    Query.Next;
  end;
  btnNext.Visible := not (SelectedList.Items.Count = 0);
end;

end.
